﻿using System;

namespace Program19
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出最长的字符串。
            string[] names = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            string max = GetLongestString(names);
            Console.WriteLine(max);

            //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
            int[] nums = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
            double avg = GetAvg(nums);
            Console.WriteLine("{0:0.00}", avg);

            //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
            int[] numbers = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            BubbleSort(numbers);
        }

        /// <summary>
        /// 取最长的字符串
        /// </summary>
        public static string GetLongestString(string[] names)
        {
            string max = names[0];
            for (var i = 0; i < names.Length; i++)
            {
                if (names[i].Length > max.Length)
                    max = names[i];
            }
            return max;
        }

        /// <summary>
        /// 求整型数组的平均值
        /// </summary>
        public static double GetAvg(int [] nums)
        {
            double sum = 0.0f;
            for(var i = 0; i < nums.Length; i++)
                sum += nums[i];
            return sum / nums.Length;
        }

        public static void BubbleSort(int[] nums)
        {
            Console.WriteLine("排序前：");
            for (var i = 0; i < nums.Length; i++)
                Console.Write(nums[i] + " ");
            Console.WriteLine();
            Console.WriteLine("排序后：");

            for(var i = 0; i < nums.Length; i++)
            {
                for(var j=0;j<nums.Length-1-i;j++)
                {
                    if (nums[j] > nums[j + 1])
                    {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }

            for (var i = 0; i < nums.Length; i++)
                Console.Write(nums[i] + " ");
            Console.WriteLine();
        }
    }
}